<template>
  <div class="in-wrap">
    <!-- 公共头引入 -->
    <header id="header">
      <section class="container">
        <!-- <h1 id="logo">
          <a href="#" title="谷粒学院">
            <img src="~/assets/img/logo.png" width="100%" alt="谷粒学院">
          </a>
        </h1> -->
        <div class="h-r-nsl">
          <ul class="nav">
            <!-- 下面这些是固定路由 -->
            <!-- 这里的页面转换有一个固定的格式 -->
            <!-- 在pages里面创建文件夹cource -->
            <!-- 在cource文件夹下创建index.vue -->
            <router-link to="/" tag="li" active-class="current" exact>
              <a>首页</a>
            </router-link>
            <router-link to="/course" tag="li" active-class="current">
              <a>课程</a>
            </router-link>
            <router-link to="/teacher" tag="li" active-class="current">
              <a>名师</a>
            </router-link>
            <router-link to="/my" tag="li" active-class="current">
              <a>个人中心</a>
            </router-link>
          </ul>
          <!-- / nav -->
          <ul class="h-r-login">
            <li v-if="!loginInfo.id" id="no-login">
              <a href="/login" title="登录">
                <em class="icon18 login-icon">&nbsp;</em>
                <span class="vam ml5">登录</span>
              </a>
              |
              <a href="/register" title="注册">
                <span class="vam ml5">注册</span>
              </a>
            </li>
            <li v-if="loginInfo.id" id="is-login-one" class="mr10">
              <a id="headerMsgCountId" href="#" title="消息">
                <em class="icon18 news-icon">&nbsp;</em>
              </a>
              <q class="red-point" style="display: none">&nbsp;</q>
            </li>
            <li v-if="loginInfo.id" id="is-login-two" class="h-r-user">
              <a href="/ucenter" title>
                <img :src="loginInfo.avatar" width="30" height="30" class="vam picImg" alt>
                <span id="userName" class="vam disIb">{{ loginInfo.nickname }}</span>
              </a>
              <a href="javascript:void(0);" title="退出" @click="logout()" class="ml5">退出</a>
            </li>
            <!-- /未登录显示第1 li；登录后显示第2，3 li -->
          </ul>
          <aside class="h-r-search">
            <form action="#" method="post">
              <label class="h-r-s-box">
                <input type="text" placeholder="输入你想学的课程" name="queryCourse.courseName" value>
                <button type="submit" class="s-btn">
                  <em class="icon18">&nbsp;</em>
                </button>
              </label>
            </form>
          </aside>
        </div>
        <aside class="mw-nav-btn">
          <div class="mw-nav-icon"></div>
        </aside>
        <div class="clear"></div>
      </section>
    </header>
    <!-- /公共头引入 -->

    <nuxt />

  </div>
</template>
<script>
// import "~/assets/css/reset.css";
// import "~/assets/css/theme.css";
// import "~/assets/css/global.css";
// import "~/assets/css/web.css";
import '~/assets/css/reset.css'
import '~/assets/css/theme.css'
import '~/assets/css/global.css'
import '~/assets/css/web.css'
import '~/assets/css/base.css'
import '~/assets/css/activity_tab.css'
import '~/assets/css/bottom_rec.css'
import '~/assets/css/nice_select.css'
import '~/assets/css/order.css'
import '~/assets/css/swiper-3.3.1.min.css'
// 加上下面的这行css样式会报错
// import "~/assets/css/pages-weixinpay.css"




import cookie from 'js-cookie';
import loginApi from '@/api/login'
export default {

  data() {
    return {
      token: '',
      loginInfo: {
        id: '',
        age: '',
        avatar: '',
        mobile: '',
        nickname: '',
        sex: '',
      }
    }
  },
  // 在页面渲染之前执行
  created() {
    // 获取路径里面token的值
    this.token=this.$route.query.token;
    console.log(this.token);
    if(this.token){
      // 判断路径里面是否有token值
      this.wxLogin();
    }
    this.showInfo();
  },
  methods: {
    // 微信登录显示的方法
    wxLogin(){
      // 把token值放到cookie里面去,注意这里修改了转换为JSON
      cookie.set('guli_token',this.token,{domain:'localhost'})
      cookie.set('guli_ucenter',JSON.stringify(this.loginInfo),{domain:'localhost'})
      // 调用接口，根据token值获取用户信息
      loginApi.getLoginInfo()
      .then(response=>{
        console.log("**********"+response.data.data.ucenterMember)
        this.loginInfo = response.data.data.ucenterMember;
        cookie.set('guli_ucenter',this.loginInfo,{domain:'localhost'})
      })
    },
    // 创建方法，从cookie中获取用户信息
    showInfo() {
      // 从cookie获取用户信息
      var userStr = cookie.get('guli_ucenter');
      // 把字符串转换为json对象的格式
      if (userStr) {
        this.loginInfo = JSON.parse(userStr);
        // this.loginInfo = JSON.parse(userStr);
      }
    },
    // 一点击退出按钮就执行这个方法
    logout() {
      // 清除cookie里面的数据
      cookie.set('guli_token', '', { domain: 'localhost' });
      cookie.set('guli_ucenter', '', { domain: 'localhost' })
      // 跳转到首页面
      window.location.href = "/";
    },
  },

};
</script>